package com.ylw.entity;

import com.ylw.inter.ReadTable;
import com.ylw.util.Constant;
import com.ylw.util.enumClass.TableHeadParameterName.ParameterName;
import org.apache.log4j.Logger;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.*;
import java.util.concurrent.Callable;

public class Response implements Callable<String>, ReadTable {
    private static final Logger logger = Logger.getLogger(Request.class);

    protected List<Parameter> parameters = new ArrayList();
    protected XWPFTable table;
    protected Service service;
    private int header = 0;//当前表头在表的哪一行


    public Response(XWPFTable table, Service service) {
        this.table = table;
        this.service = service;
    }

    @Override
    public String read() {
        if (table == null) {
            return Constant.NULL;
        }
        for (XWPFTableRow row : table.getRows()) {
            String name = row.getCell(0).getText().trim();
            String type = row.getCell(1).getText().trim();
            String remark = row.getCell(2).getText().trim();
            Parameter parameter = new Parameter(name, type, remark);
            logger.info(parameter);

            parameters.add(parameter);
        }
        return Constant.FINISHED;
    }


    /**
     * 判断是否为表头
     * 依据第一列的中文字符判断
     *
     * @param row
     * @return
     */
    private boolean isTableHeader(XWPFTableRow row, int col, Parameter parameter) {
        XWPFTableCell cell = row.getCell(col);
        if (cell == null) {
            return false;
        }
        if (ParameterName.getParamterName(cell.getText().trim())!=null) {
            return true;
        }
        return false;
    }

    @Override
    public String call() throws Exception {
        return read();
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"parameters\":").append(parameters);
        sb.append(",\"service\":").append(service);
        sb.append('}');
        return sb.toString();
    }
}
